package com.hansheng.action.motherSheet;

import com.hansheng.entity.MotherSheetEntity;
import com.hansheng.entity.SheetEntity;
import com.hansheng.service.impl.MotherSheetServiceImpl;
import com.hansheng.service.impl.SheetServiceImpl;
import com.hansheng.util.InitialVariableByTimer;
import com.hansheng.util.JsonHelper;
import com.hansheng.util.PrintWriterHelper;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.hansheng.util.JsonHelper.OPER_FAILED;

/**
 * Created by GoldenMan on 2017/5/12.
 */
public class SaveMotherSheetAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
    private MotherSheetServiceImpl motherSheetService;
    private SheetServiceImpl sheetService;
    private HttpServletResponse httpServletResponse;
    private HttpServletRequest httpServletRequest;
    private int mid;
    private int materialDetect;
    private int operator;
    private int length;
    private int width;
    private int height;
    private int count;
    private String weightThe;
    private String weightAct;
    public void setMotherSheetService(MotherSheetServiceImpl motherSheetService) {
        this.motherSheetService = motherSheetService;
    }

    public void setSheetService(SheetServiceImpl sheetService) {
        this.sheetService = sheetService;
    }

    public int getMid() {
        return mid;
    }

    public void setMid(int mid) {
        this.mid = mid;
    }

    public int getMaterialDetect() {
        return materialDetect;
    }

    public void setMaterialDetect(int materialDetect) {
        this.materialDetect = materialDetect;
    }

    public int getOperator() {
        return operator;
    }

    public void setOperator(int operator) {
        this.operator = operator;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getWeightThe() {
        return weightThe;
    }

    public void setWeightThe(String weightThe) {
        this.weightThe = weightThe;
    }

    public String getWeightAct() {
        return weightAct;
    }

    public void setWeightAct(String weightAct) {
        this.weightAct = weightAct;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.httpServletRequest = request;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.httpServletResponse = response;
        httpServletResponse.setContentType("text/html;charset=utf-8");
        httpServletResponse.setHeader("Cache-Control", "no-cache");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
    }

    @Override
    public String execute() throws Exception {
        try {
            // Created by GoldenMan at 2017/5/30    TODO:来料检测
            if(getOperator()<=0||getMaterialDetect()<=0)
                throw new Exception();
            MotherSheetEntity motherSheetEntity = new MotherSheetEntity();
            motherSheetEntity.setMid(getMid());
            motherSheetEntity.setMsNo(getMsNo());
            motherSheetEntity.setMaterialDete(getMaterialDetect());
            motherSheetEntity.setOperator(getOperator());
            motherSheetEntity.setLength(getLength());
            motherSheetEntity.setWidth(getWidth());
            motherSheetEntity.setHeight(getHeight());
            motherSheetEntity.setCount(getCount());
            motherSheetEntity.setWeightThe(Double.parseDouble(getWeightThe()));
            motherSheetEntity.setWeightAct(Double.parseDouble(getWeightAct()));
            motherSheetEntity.setStatus(1);
            boolean flag = motherSheetService.saveMotherSheet(motherSheetEntity);
            if (!flag)
                return ERROR;
            SheetEntity sheetEntity = new SheetEntity();
            sheetEntity.setMsid(motherSheetEntity.getMsid());
            sheetEntity.setOperator(getOperator());
            sheetEntity.setLength(getLength());
            sheetEntity.setWidth(getWidth());
            sheetEntity.setHeight(getHeight());
            sheetEntity.setCount(getCount());
            sheetEntity.setWeightThe(Double.parseDouble(getWeightThe()));
            sheetEntity.setWeightAct(Double.parseDouble(getWeightAct()));
            sheetEntity.setStatus(1);
            flag &= sheetService.saveSheet(sheetEntity);
            PrintWriterHelper.send(flag? JsonHelper.successJSONBuilder():JsonHelper.errJSONBulider(1,OPER_FAILED));
        }catch (Exception e){
            e.printStackTrace();
            PrintWriterHelper.send(JsonHelper.errJSONBulider(1,OPER_FAILED));
        }
        return null;
    }

    private String getMsNo(){
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(now) + "-" + InitialVariableByTimer.motherSheetCounter++;
    }
}
